package com.bignippleboy.recordthelife;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;

import com.bignippleboy.recordthelife.constants.Constants;

public class AudioManager {

	public static AudioManager audioManager;

	private final String AUDIO_EXT = ".amr";
//	LifeRecordItem item;
//
//	public LifeRecordItem getItem() {
//		return item;
//	}
//
//	public void setItem(LifeRecordItem item) {
//		this.item = item;
//	}

	public static AudioManager getInstance() {
		if (audioManager == null)
			audioManager = new AudioManager();
		return audioManager;
	}

	public String saveAudio(String audio) {

		Date curDate = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateString = sdf.format(curDate);
		sdf = new SimpleDateFormat("hhmmss");
		String timeString = sdf.format(curDate);

		String basicPathString = Constants.BASIC_PATH + dateString;
		if (Constants.isSDcardWork()) {
			File path = new File(basicPathString);
			if (!path.exists())
				path.mkdirs();
		}

		String audioPathString = basicPathString + "/" + timeString + AUDIO_EXT;
//		Constants.item.setAudio(audioPathString);
		
//		write(audioPathString, audio);
//		if (Constants.item != null)
//			Constants.m_MyDataBaseAdapter.updateData(Constants.item.getRowId(), Constants.item);
//		Constants.m_MyDataBaseAdapter.close();
		return audioPathString;
	}

	private void write(String fileName, String resultFile) {
		try {
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter(fileName)));
			out.print(resultFile);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
